package com.GUIWork;

import java.awt.BorderLayout;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.dbConnection.theConnection;

public class searchResults extends JFrame {

	private int COLUMNS = 6;
	
	public searchResults(theConnection conn){
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JLabel lblItemId = new JLabel("Item ID");
		lblItemId.setBounds(10, 5, 36, 14);
		panel.add(lblItemId);
		
		JLabel lblItemName = new JLabel("Item Name");
		lblItemName.setBounds(56, 5, 52, 14);
		panel.add(lblItemName);
		
		JLabel lblPrice = new JLabel("Current Bid");
		lblPrice.setBounds(133, 5, 54, 14);
		panel.add(lblPrice);
		
		JLabel lblHighBidder = new JLabel("High Bidder");
		lblHighBidder.setBounds(207, 5, 54, 14);
		panel.add(lblHighBidder);
		
		JLabel lblGetItNow = new JLabel("Get It Now Price");
		lblGetItNow.setBounds(282, 5, 78, 14);
		panel.add(lblGetItNow);
		
		JLabel lblAuctionEnds = new JLabel("Auction Ends");
		lblAuctionEnds.setBounds(370, 5, 62, 14);
		panel.add(lblAuctionEnds);
		ArrayList<String> results = new ArrayList<String>();
		try {
			results = conn.getLastQuery(COLUMNS);
		} catch (SQLException e) {
			System.out.println("Couldn't get the search results");
			e.printStackTrace();
			}
		if(results.size()==0){
			System.out.println("No results found");
		}
		for(int i = 0; i < results.size(); i++){
			//make labels
			JLabel idlabel = new JLabel();
			JLabel namelabel = new JLabel();
			JLabel currbidLabel = new JLabel();
			JLabel highbidLabel = new JLabel();
			JLabel nowpriceLabel = new JLabel();
			JLabel endauctionLabel = new JLabel();
			
			//set bounds (location)
			
			//put labels on panel
			panel.add(idlabel);
			panel.add(namelabel);
			//if there IS a high bid, make labels
			if(results.get(i*6 + 2)!=null){
				panel.add(currbidLabel);
				panel.add(highbidLabel);
				
				currbidLabel.setText(results.get(i*6 +2));
				highbidLabel.setText(results.get(i*6+3));
			}
			if(results.get(i*6 + 4)!=null){
				panel.add(nowpriceLabel);
				
				nowpriceLabel.setText(results.get(i*6 + 4));
			}
			panel.add(endauctionLabel);
			
			//set labels
			idlabel.setText(results.get(i*6));
			namelabel.setText(results.get(i*6 + 1));
			endauctionLabel.setText(results.get(i*6 + 5));
		}
	}
}
